<?php
/**
 * Created by PhpStorm.
 * User: leo
 * Date: 16/5/23
 * Time: 下午2:30
 * Describe:
 */

namespace console\models;

use \common\models\tmall1\OrderInfo as TOrderInfo;
use \common\models\tmall1\OrderGoods as TOrderGoods;
use services\models\OrderGoods;
use services\models\OrderInfo;
use yii\base\Exception;
use Yii;
use common\helper\ArrayHelper;

class Order extends Object
{
    public $order_info_id = null;

    public static function info($msg)
    {
        Yii::info(getmypid() . ' ' . $msg, 'order');
    }

    public function run()
    {
        if($this->writeToTmallOrderInfo()){
            echo $this->order_info_id, "\n";
            $this->writeToTmallOrderGoods();
            $this->syncGoodsName();
            $this->syncSpecDesc();
        }

    }

    public function writeToTmallOrderInfo()
    {
        if (!$this->order_info_id) {
            Yii::info("order_info_id is null!");
            return false;
        }
        $order = OrderInfo::find()->filterWhere(['order_info.order_info_id' => $this->order_info_id])
                          ->innerJoinWith('orderExpress')->one();
        if (empty($order)) {
            Yii::info("order info id:{$this->order_info_id} not found in d_try order_info!");
            return false;
        }
        $oldOrder                = new TOrderInfo();
        try {
            $oldOrder->uid           = $order->order_info_id;
            $oldOrder->ordersn       = $order->order_sn;
            $oldOrder->member_uid    = $order->bestry_uid;
            $oldOrder->type          = $order->type;
            $oldOrder->addtime       = strtotime($order->create_time);
            $oldOrder->status        = $order->status;
            $oldOrder->pay_result    = $order->pay_status;
            $oldOrder->pay_id        = $order->pay_id;
            $oldOrder->pay_name      = $order->pay_name;
            $oldOrder->defaultbank   = $order->bank;
            $oldOrder->goods_amount  = $order->goods_amount;
            $oldOrder->order_amount  = $order->order_amount;
            $oldOrder->member_money  = $order->member_money;
            $oldOrder->reduce_amount = $order->reduce_amount;
            $oldOrder->coupon_id     = $order->coupon_id;
            $oldOrder->coupon_code   = $order->coupon_code;
            $oldOrder->coupon_amount = $order->coupon_amount;
            $oldOrder->ship_amount   = $order->ship_amount;
            $oldOrder->is_invoice    = $order->is_invoice;
            $oldOrder->remark        = $order->remark;
            $oldOrder->modify_time   = $order->update_time;
            $oldOrder->jfs           = $order->try_coin;
            $oldOrder->origin        = $order->source;


            $oldOrder->consignee     = $order->orderExpress->consignee;
            $oldOrder->address       = $order->orderExpress->address;
            $oldOrder->s1            = $order->orderExpress->province;
            $oldOrder->s2            = $order->orderExpress->city;
            $oldOrder->s3            = $order->orderExpress->county;
            $oldOrder->address       = $order->orderExpress->address;
            $oldOrder->tel           = $order->orderExpress->telephone;
            $oldOrder->cell          = $order->orderExpress->mobile_phone;
            $oldOrder->delivery_id   = $order->orderExpress->delivery_id;
            $oldOrder->delivery_code = $order->orderExpress->delivery_code;
            $oldOrder->delivery_time = $order->orderExpress->delivery_time;
            $oldOrder->modify_time   = time();
            $oldOrder->admin_memo    = '';
            $oldOrder->save();
            if ($oldOrder->hasErrors()) {
                static::info("d_try to tmall1 Failed:{$order->order_info_id} order_sn:{$order->order_sn} " . ArrayHelper::arrayToString($oldOrder->getErrors()));
                return false;
            } else {
                static::info("d_try to tmall1 id:{$order->order_info_id} order_sn:{$order->order_sn}");
                return true;
            }

        } catch (Exception $e) {
            echo $this->order_info_id, "bb\n";
            static::info("d_try to tmall1 Failed:{$order->order_info_id} order_sn:{$order->order_sn} " . $e->getMessage());
            return false;
        } finally {
            unset($oldOrder);
        }

    }

    public function writeToTmallOrderGoods()
    {
        $order_goods = OrderGoods::find()->filterWhere(['order_info_id' => $this->order_info_id])->all();
        if(empty($order_goods)){
            Yii::info("order info id:{$this->order_info_id} order goods not found in d_try order_goods");
        }
        /** @var OrderGoods $od */
        foreach ($order_goods as $od) {
            try {
                /** @var TOrderGoods $tog */
                $tog                = new TOrderGoods();
                $tog->order_id      = $od->order_info_id;
                $tog->goods_id      = $od->goods_id;
                $tog->product_id    = $od->product_id;
                $tog->member_uid    = $od->bestry_uid;
                $tog->buy_number    = $od->buy_number;
                $tog->buy_price     = $od->buy_price;
                $tog->addtime       = strtotime($od->create_time);
                $tog->free_order_id = $od->free_gift;
                $tog->comment_id    = $od->commented;
                $tog->act_id        = $od->activity_id;
                $tog->price_type    = $od->price_type;
                $tog->jfs           = $od->try_coin;
                $tog->save();
                if ($tog->hasErrors()) {
                    static::info("d_try to tmall1 order_goods Failed:{$od->order_info_id} product_id:{$od->product_id} " . ArrayHelper::arrayToString($tog->getFirstErrors()));
                }else{
                    static::info("d_try to tmall1 id:{$od->order_info_id} order_sn:{$od->order_sn}, product_id:{$od->product_id}");
                }
            } catch (Exception $e) {
                static::info("d_try to tmall1 order_goods Failed:{$od->order_info_id} product_id:{$od->product_id} " . $e->getMessage());
            } finally {
                unset($tog);
            }
        }
    }


    public function syncGoodsName()
    {
        Yii::$app->db->createCommand("UPDATE tmall1.order_goods g, d_try.goods_detail gd
            SET g.goods_name = gd.goods_name
            WHERE g.goods_id = gd.goods_id AND g.goods_name = '' AND order_id ={$this->order_info_id};")
                     ->execute();
    }

    public function syncSpecDesc()
    {
        Yii::$app->db->createCommand("UPDATE tmall1.order_goods g, d_try.product_detail gd
            SET g.spec_desc = gd.spec_desc
            WHERE g.product_id = gd.product_id AND g.spec_desc = '' and gd.spec_desc<>'' AND order_id ={$this->order_info_id};")
                     ->execute();
    }
}